from Case.python_ui.Common.basepage import Basepage
import time, pytest
from Case.python_ui.Common.commontool import CommonTool
from selenium.webdriver.common.action_chains import ActionChains
from Case.python_ui.PageLocate.admin_my_resource_locator import AdminMyResourceLocator
from selenium.webdriver.common.by import By
from datetime import datetime
from Case.python_ui.PageLocate.user_slb_locator import UserSLBLocator
from Case.python_ui.Pageobject.user_slb_pages import UserSLBPages
from Case.python_ui.PageLocate.product_management_locator import ProductManagementLocator
from Case.python_ui.Pageobject.admin_product_managent_pages import ProductManagementPages
from Case.python_ui.PageLocate.user_my_resource_locator import UserMyResourceLocator
from Case.python_ui.Common.business_common_tool import BusinessCommonTools

class AdminMyResourcePage(Basepage):
    # 进入My Resource界面
    def enter_my_resource_page(self):
        time.sleep(2)
        doc = "进入My Resource菜单"
        js_object = 'document.getElementsByClassName("cre-sider-menu cre-sider-menu-open")[0].scrollTop=900'
        self.excute_js(js_object);

        self.wait_elevisble(AdminMyResourceLocator.my_resource_menu, doc)
        self.click_element(AdminMyResourceLocator.my_resource_menu, doc)

    # 进入My Resource界面
    def enter_my_resource_page1(self):
        time.sleep(2)
        doc = "进入My Resource菜单"
        js_object = 'document.getElementsByClassName("cre-sider-menu cre-sider-menu-open")[0].scrollTop=900'
        self.excute_js(js_object);

        self.wait_elevisble(AdminMyResourceLocator.my_resource_menu2, doc)
        self.click_element(AdminMyResourceLocator.my_resource_menu2, doc)

    #判断是否包含元素
    def check_contain_str(self, string, except_string):
        if except_string in string:
            assert True
        else:
            assert False , '实际值:' + string + '期望值:' + except_string

    # 校验某个属性值是否符合预期
    def check_attribute(self, real_result, except_result):
        if real_result == except_result:
            assert True
        else:
            assert False, '实际值:' + real_result + '期望值:' + except_result


    def get_prod_name_to_click(self,prod_name):
        doc='获取产品名称然后点击'
        prod_list=self.get_elements(ProductManagementLocator.prod_list,doc)
        time.sleep(2)
        for prod in prod_list:
            if prod.text == prod_name:
                prod.click()
        time.sleep(2)


    #product name查询功能
    def search_product_name(self):
        # 当前查询对应用户已申请资源
        doc = '按product name搜索查询'
        result = 'Service'
        self.enter_my_resource_page()
        time.sleep(5)
        self.wait_elevisble(AdminMyResourceLocator.search_select_botton, doc)
        self.click_element(AdminMyResourceLocator.search_select_botton, doc)
        time.sleep(2)
        self.wait_elevisble(AdminMyResourceLocator.search_product_name, doc)
        self.click_element(AdminMyResourceLocator.search_product_name, doc)
        time.sleep(2)
        self.wait_elevisble(AdminMyResourceLocator.product_name_input, doc)
        self.send_keys(AdminMyResourceLocator.product_name_input, result, doc)
        time.sleep(2)
        self.click_element(AdminMyResourceLocator.search_result, doc)
        time.sleep(2)
        self.wait_elevisble(AdminMyResourceLocator.search_botton, doc)
        self.click_element(AdminMyResourceLocator.search_botton, doc)
    # 检查是否可以模糊查询
        time.sleep(2)
        self.check_contain_str(self.get_element(AdminMyResourceLocator.first_search_result, doc).text, result)


    # 功能功能验证
    def search(self):
        # 当前查询对应用户已申请资源
        doc = '搜索功能验证'
        #邮件搜索
        result = 'Etest1@sigma.com'
        start_time_input = '10-12-2023'
        end_time_input = '10-12-2024'
        self.enter_my_resource_page()
        time.sleep(5)
        self.wait_elevisble(AdminMyResourceLocator.search_select_botton, doc)
        self.click_element(AdminMyResourceLocator.search_select_botton, doc)
        time.sleep(2)
        self.wait_elevisble(AdminMyResourceLocator.email_input, doc)
        self.send_keys(AdminMyResourceLocator.email_input, 'Etest1@sigma.com', doc)
        time.sleep(2)
        self.wait_elevisble(AdminMyResourceLocator.start_time_select, doc)
        self.send_keys(AdminMyResourceLocator.start_time_select, start_time_input, doc)
        time.sleep(2)
        self.click_element(AdminMyResourceLocator.end_time_select, doc)
        self.send_keys(AdminMyResourceLocator.end_time_select, end_time_input, doc)
        time.sleep(2)
        self.wait_elevisble(AdminMyResourceLocator.search_botton, doc)
        self.click_element(AdminMyResourceLocator.search_botton, doc)
        time.sleep(2)
        js_object = 'document.getElementsByClassName("antd4-table-content")[0].scrollTo(1000,10000)'
        self.excute_js(js_object)
        self.check_attribute(self.get_element(AdminMyResourceLocator.first_search_result1, doc).text, result)
        #ID搜索
        self.wait_elevisble(AdminMyResourceLocator.search_select_botton, doc)
        self.click_element(AdminMyResourceLocator.search_select_botton, doc)
        time.sleep(2)
        self.wait_elevisble(AdminMyResourceLocator.search_id, doc)
        self.click_element(AdminMyResourceLocator.search_id, doc)
        self.wait_elevisble(AdminMyResourceLocator.instance_id_input, doc)
        self.send_keys(AdminMyResourceLocator.instance_id_input, 'eip-nj3c7sin37c915vetr7i1', doc)
        time.sleep(2)
        self.wait_elevisble(AdminMyResourceLocator.search_botton, doc)
        self.click_element(AdminMyResourceLocator.search_botton, doc)
        time.sleep(2)
        # Organization搜索
        result4 = 'kawaii'
        self.wait_elevisble(AdminMyResourceLocator.search_select_botton, doc)
        self.click_element(AdminMyResourceLocator.search_select_botton, doc)
        time.sleep(2)
        self.wait_elevisble(AdminMyResourceLocator.search_organization, doc)
        self.click_element(AdminMyResourceLocator.search_organization, doc)
        time.sleep(2)
        self.wait_elevisble(AdminMyResourceLocator.organization_input, doc)
        self.send_keys(AdminMyResourceLocator.organization_input, result4, doc)
        time.sleep(2)
        self.click_element(AdminMyResourceLocator.search_result1, doc)
        time.sleep(2)
        self.wait_elevisble(AdminMyResourceLocator.search_botton, doc)
        self.click_element(AdminMyResourceLocator.search_botton, doc)
        js_object = 'document.getElementsByClassName("antd4-table-content")[0].scrollTo(1000,10000)'
        self.excute_js(js_object)
        time.sleep(2)
        self.check_attribute(self.get_element(AdminMyResourceLocator.first_search_result5, doc).text, result4)


    # 列表字段
    def view_list(self):
        doc = '查看列表字段'
        self.enter_my_resource_page()
        time.sleep(5)
        self.wait_elevisble(AdminMyResourceLocator.Instance_ID, doc)
        self.check_attribute(self.get_element(AdminMyResourceLocator.Instance_ID, doc).text, 'Account Type')
        self.wait_elevisble(AdminMyResourceLocator.Product_Name, doc)
        self.check_attribute(self.get_element(AdminMyResourceLocator.Product_Name, doc).text, 'Order ID')
        self.wait_elevisble(AdminMyResourceLocator.Region, doc)
        self.check_attribute(self.get_element(AdminMyResourceLocator.Region, doc).text, 'Product')
        self.wait_elevisble(AdminMyResourceLocator.Billing_Method, doc)
        self.check_attribute(self.get_element(AdminMyResourceLocator.Billing_Method, doc).text, 'Order Type')
        self.wait_elevisble(AdminMyResourceLocator.Created_Time, doc)
        self.check_attribute(self.get_element(AdminMyResourceLocator.Created_Time, doc).text, 'Created Time (UTC+08:00)')
        self.wait_elevisble(AdminMyResourceLocator.Expired_Time, doc)
        self.check_attribute(self.get_element(AdminMyResourceLocator.Expired_Time, doc).text, 'Order Status')
        self.wait_elevisble(AdminMyResourceLocator.Product_Type1, doc)
        self.check_attribute(self.get_element(AdminMyResourceLocator.Product_Type1, doc).text,'Billing Method')
        self.wait_elevisble(AdminMyResourceLocator.Product_Type2, doc)
        self.check_attribute(self.get_element(AdminMyResourceLocator.Product_Type2, doc).text, 'Gross Amount')
        time.sleep(2)
        js_object = 'document.getElementsByClassName("antd4-table-content")[0].scrollTo(1000,10000)'
        self.excute_js(js_object);
        time.sleep(2)
        # self.wait_elevisble(AdminMyResourceLocator.Enable_Auto_renewal, doc)
        # self.check_attribute(self.get_element(AdminMyResourceLocator.Enable_Auto_renewal, doc).text, 'Enable Auto-renewal')
        # self.wait_elevisble(AdminMyResourceLocator.Renewal_Status, doc)
        # self.check_attribute(self.get_element(AdminMyResourceLocator.Renewal_Status, doc).text,'Renewal Status')
        # self.wait_elevisble(AdminMyResourceLocator.Auto_Scaling, doc)
        # self.check_attribute(self.get_element(AdminMyResourceLocator.Auto_Scaling, doc).text, 'Auto Scaling')
        # self.wait_elevisble(AdminMyResourceLocator.Oraganization, doc)
        # self.check_attribute(self.get_element(AdminMyResourceLocator.Oraganization, doc).text, 'Organization')
        # self.wait_elevisble(AdminMyResourceLocator.Account_Email, doc)
        # self.check_attribute(self.get_element(AdminMyResourceLocator.Account_Email, doc).text, 'Account Email')


    def create_time_check(self):
        doc = 'create time排序功能校验'
        self.enter_my_resource_page()
        time.sleep(5)
        #正序
        self.click_element(AdminMyResourceLocator.create_time_button, doc)
        time.sleep(2)
        self.wait_elevisble(AdminMyResourceLocator.list_first_line, doc)
        order_creation_times = []
        eles = self.get_elements(AdminMyResourceLocator.create_time, doc)
        # 获取订单创建时间数组
        for ele in eles:
            order_creation_times.append(ele.text)
        print(order_creation_times)
        # 将时间字符串转换为datetime对象，并按照时间顺序排序
        list_times = sorted(map(lambda time_str: datetime.strptime(time_str, "%d-%m-%Y %H:%M:%S"), order_creation_times), reverse=False)
        print(list_times)
        # 将datetime对象列表转换回字符串列表，并按照"%Y-%m-%d %H:%M:%S"格式进行格式化
        dates_as_str = [date.strftime("%d-%m-%Y %H:%M:%S") for date in list_times]
        print(dates_as_str)
        assert dates_as_str == order_creation_times
        # 倒序
        time.sleep(5)
        self.wait_elevisble(AdminMyResourceLocator.create_time_button, doc)
        self.click_element(AdminMyResourceLocator.create_time_button, doc)
        time.sleep(5)
        self.wait_elevisble(AdminMyResourceLocator.list_first_line, doc)
        order_creation_times2 = []
        eles2 = self.get_elements(AdminMyResourceLocator.create_time, doc)
        # 获取订单创建时间数组
        for ele in eles2:
            order_creation_times2.append(ele.text)
        print(order_creation_times2)
        # 将时间字符串转换为datetime对象，并按照时间顺序排序
        list_times2 = sorted(map(lambda time_str: datetime.strptime(time_str, "%d-%m-%Y %H:%M:%S"), order_creation_times2), reverse=True)
        print(list_times2)
        # 将datetime对象列表转换回字符串列表，并按照"%Y-%m-%d %H:%M:%S"格式进行格式化
        dates_as_str2 = [date.strftime("%d-%m-%Y %H:%M:%S") for date in list_times2]
        print(dates_as_str2)
        assert dates_as_str2 == order_creation_times2


    def release_resource(self, instance_id):
        doc = 'admin端释放资源'
        self.enter_my_resource_page1()
        time.sleep(5)
        self.click_element(AdminMyResourceLocator.search_select_botton, doc)
        time.sleep(2)
        self.click_element(AdminMyResourceLocator.search_id, doc)
        time.sleep(2)
        self.send_keys(AdminMyResourceLocator.instance_id_input, instance_id, doc)
        self.wait_elevisble(AdminMyResourceLocator.my_resource_search_botton, doc)
        self.click_element(AdminMyResourceLocator.my_resource_search_botton, doc)
        time.sleep(5)
        self.click_element(AdminMyResourceLocator.release_button, doc)
        time.sleep(2)
        self.switch_to_new_window()
        self.wait_elevisble(ProductManagementLocator.loading_wait, doc)
        self.wait_eleinvisble(ProductManagementLocator.loading_wait, doc)
        time.sleep(2)
        self.wait_elevisble(ProductManagementLocator.release_button, doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.release_button, doc)
        self.wait_elevisble(ProductManagementLocator.buy_now_button, doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.buy_now_button, doc)
        self.wait_elevisble(ProductManagementLocator.release_successful, doc)
        self.driver.close()
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[0])




    def release_page_note_check(self, instance_id):
        doc = '释放界面字段校验'
        note = '*Note:Please confirm if you want to delete the instance, this operation cannot be resumed.'
        self.enter_my_resource_page1()
        time.sleep(5)
        self.click_element(AdminMyResourceLocator.search_select_botton, doc)
        time.sleep(2)
        self.click_element(AdminMyResourceLocator.search_id, doc)
        time.sleep(2)
        self.send_keys(AdminMyResourceLocator.instance_id_input, instance_id, doc)
        self.wait_elevisble(AdminMyResourceLocator.my_resource_search_botton, doc)
        self.click_element(AdminMyResourceLocator.my_resource_search_botton, doc)
        time.sleep(5)
        self.click_element(AdminMyResourceLocator.release_button, doc)
        time.sleep(2)
        self.switch_to_new_window()
        self.wait_elevisble(ProductManagementLocator.loading_wait, doc)
        self.wait_eleinvisble(ProductManagementLocator.loading_wait, doc)
        time.sleep(2)
        self.wait_elevisble(AdminMyResourceLocator.release_page_note, doc)
        self.check_attribute(self.get_element(AdminMyResourceLocator.release_page_note, doc).text, note)
        self.wait_elevisble(ProductManagementLocator.release_button, doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.release_button, doc)
        self.wait_elevisble(ProductManagementLocator.buy_now_button, doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.buy_now_button, doc)
        self.wait_elevisble(ProductManagementLocator.release_successful, doc)



    def release_page_check_order(self, instance_id):
        doc = '释放成功之后的两个按钮跳转校验'
        self.enter_my_resource_page1()
        time.sleep(5)
        self.click_element(AdminMyResourceLocator.search_select_botton, doc)
        time.sleep(2)
        self.click_element(AdminMyResourceLocator.search_id, doc)
        time.sleep(2)
        self.send_keys(AdminMyResourceLocator.instance_id_input, instance_id, doc)
        self.wait_elevisble(AdminMyResourceLocator.my_resource_search_botton, doc)
        self.click_element(AdminMyResourceLocator.my_resource_search_botton, doc)
        time.sleep(5)
        self.click_element(AdminMyResourceLocator.release_button, doc)
        time.sleep(2)
        self.switch_to_new_window()
        self.wait_elevisble(ProductManagementLocator.loading_wait, doc)
        self.wait_eleinvisble(ProductManagementLocator.loading_wait, doc)
        time.sleep(2)
        self.wait_elevisble(ProductManagementLocator.release_button, doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.release_button, doc)
        self.wait_elevisble(ProductManagementLocator.buy_now_button, doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.buy_now_button, doc)
        self.wait_elevisble(ProductManagementLocator.pay_successful, doc)
        time.sleep(2)
        self.click_element(AdminMyResourceLocator.check_order_button, doc)
        time.sleep(5)
        self.check_attribute(self.get_element(AdminMyResourceLocator.orders_page_title, doc).text, 'Orders')




    def release_my_notification(self):
        doc = '释放成功消息通知'
        self.wait_elevisble(AdminMyResourceLocator.my_notification_menu, doc)
        self.click_element(AdminMyResourceLocator.my_notification_menu, doc)
        time.sleep(5)
        self.check_attribute(self.get_element(AdminMyResourceLocator.notification_content, doc).text, 'Release order created successfully')


    def release_orders_detail(self,instance_id):
        doc = '释放成功之后的order生成'
        self.enter_my_resource_page()
        time.sleep(5)
        self.click_element(AdminMyResourceLocator.search_select_botton, doc)
        time.sleep(2)
        self.click_element(AdminMyResourceLocator.search_id, doc)
        time.sleep(2)
        self.send_keys(AdminMyResourceLocator.instance_id_input, instance_id, doc)
        self.wait_elevisble(AdminMyResourceLocator.search_botton, doc)
        self.click_element(AdminMyResourceLocator.search_botton, doc)
        time.sleep(5)
        self.click_element(AdminMyResourceLocator.release_button, doc)
        time.sleep(2)
        self.switch_to_new_window()
        self.wait_elevisble(ProductManagementLocator.loading_wait, doc)
        self.wait_eleinvisble(ProductManagementLocator.loading_wait, doc)
        time.sleep(2)
        self.wait_elevisble(ProductManagementLocator.release_button, doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.release_button, doc)
        self.wait_elevisble(ProductManagementLocator.buy_now_button, doc)
        time.sleep(2)
        self.click_element(ProductManagementLocator.buy_now_button, doc)
        self.wait_elevisble(ProductManagementLocator.pay_successful, doc)
        time.sleep(2)
        self.click_element(AdminMyResourceLocator.check_order_button, doc)
        time.sleep(5)
        self.check_attribute(self.get_element(AdminMyResourceLocator.order_type, doc).text, 'Release')





